<%-- Copyright (c) 2010 Canadensys --%>
<%-- JSP to handle Ajax call to create a DarwinCore archive file --%>
<%-- TODO : combine parameters handling with csv.jsp --%>
<%@ page import="java.io.BufferedReader" %>
<%@ page import="java.io.BufferedWriter" %>
<%@ page import="java.io.DataInputStream" %>
<%@ page import="java.io.File" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.io.FileOutputStream" %>
<%@ page import="java.io.OutputStreamWriter" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Collection" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.HashSet" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.UUID" %>
<%@ page import="java.util.Set" %>
<%@ page import="java.util.Vector" %>
<%@ page import="net.canadensys.dataportal.vascan.downloads.DarwinCoreArchiveManager" %>
<%@ page import="net.canadensys.dataportal.vascan.config.impl.InstallationConfig" %>
<%@ page import="net.canadensys.dataportal.vascan.config.ApplicationConfig" %>
<%@ page import="net.canadensys.dataportal.utils.HibernateUtil" %>
<%@ include file="../_inc/functions.jsp" %>
<%
try{
    out.clear();
    out.flush();
    org.hibernate.Session hibernateSession = HibernateUtil.getSessionFactory().getCurrentSession();
    hibernateSession.beginTransaction();
    
    // archive file name;
    String filename = "";
    if(session.getAttribute("filename") != null)
        filename = (String)session.getAttribute("filename");
    
    // list of taxon id to include in the darwin core archive
    /* request params */
    /* provinces */
    String[] province = null;
    if(request.getParameterValues("province") != null)
     province = request.getParameterValues("province");
    
    /* combination */
    String combination = null;
    if(request.getParameterValues("combination") != null)
        combination = request.getParameter("combination");  
    
    /* habitus */
    String habit = null;
    if(request.getParameter("habit") != null)
        habit = request.getParameter("habit");
    
    /* taxonid */
    int taxon = -1;
    String taxonId = "";
    if(request.getParameterValues("taxon") != null){
    	taxonId = request.getParameter("taxon");
        if(!taxonId.equals(""))
            taxon = Integer.valueOf(request.getParameter("taxon"));
    }
    
    /* distribution */
    String[] distributionStatus = null;
    if(request.getParameterValues("status") != null)
    	distributionStatus = request.getParameterValues("status");
    
    /* rank */
    String[] rank = null;
    if(request.getParameterValues("rank") != null)
        rank = request.getParameterValues("rank");
    
    /* display hybrids */
    String nohybrids = null;
    if(request.getParameter("nohybrids") != null)
        nohybrids = request.getParameter("nohybrids");
    String hybrids = null;
    if(request.getParameter("hybrids") != null)
        hybrids = request.getParameter("hybrids");
    
    /* sort */
    String sort = null;
    if(request.getParameter("sort") != null)
        sort = request.getParameter("sort");
    

    if(habit == "" && habit == null){
    	habit = "all";
    }

    if(combination == "" && combination == null){
    	combination = "anyof";
    }

    if(distributionStatus == null){
    	String statuses[] = {"introduced","native","ephemeral","excluded","extirpated","doubtful"};
    	distributionStatus = statuses;
    }

    if(sort == "" && sort == null){
    	sort = "taxonomically";
    }
    
    // hybrids checkbox
    if((nohybrids == null || nohybrids == "") && (hybrids == null || hybrids == "")){
        hybrids = "true";
    }
    else{
        if((nohybrids != "" && nohybrids != null) && (hybrids == null || hybrids == "")){
               hybrids = null;         
        }
        else{
            hybrids = "true";   
        }
    }
    
    DarwinCoreArchiveManager dcaManager = new DarwinCoreArchiveManager(_bundle,null);
    String destinationFilePath = InstallationConfig.getInstance().get(InstallationConfig.GENERATED_CONTENT_DIR_KEY) + filename;
    String metaXMLFilePath = ApplicationConfig.getMetaXMLFilePath();
    boolean success = dcaManager.generateDarwinCoreArchive(destinationFilePath, metaXMLFilePath, province, combination, habit, taxon, distributionStatus, rank, hybrids, sort);
    
	//we should return a JSON string and include the file name in case the filename was not available in the session.
    if(success){
    	out.write("success");
    }
    else{
    	out.write("error");
    }
}
catch(Exception e){
	e.printStackTrace();
	out.write("error");
}
%>